from flask import Flask,render_template,views,request,session,redirect,url_for

app=Flask(__name__)
class Loginview(views.MethodView):
    def __jump(self,msg=None):
        return render_template('login.html',msg=msg)
    def get(self):
        msg=request.args.get('msg')
        return self.__jump(msg)
    def post(self):
        uname=request.form.get('uname')
        passwd=request.form.get('password')
        if uname=='lnb' and passwd=='123':
            session['uname']=uname
            return render_template('index.html')
        else:
            return self.__jump(msg='用户名或密码错误')

@app.route('/index/')
def index():
    uname=session.get('uname')
    if uname :
        return '这是个人主页'
    return redirect(url_for('login',msg='请先登录'))

app.add_url_rule('/login/',view_func=Loginview.as_view('login'))

if __name__=='__main__':
    app.secret_key='fsdfsfs'
    app.run(debug=True)